package com.easygoods.tasks;

import com.easygoods.service.OrderService;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by 许崇雷 on 2019-03-01.
 */
@CommonsLog
@Component
public class OrderExpiredScheduling {
    @Autowired
    private OrderService orderService;

    //每 10 分钟执行一次
    @Scheduled(fixedDelay = 10 * 60 * 1000)
    public void queryUnsettledOrders() {
        log.info("======未支付订单过期任务开始");
        List<String> orderIdList = this.orderService.getExpiredOrderIdList();
        for (String orderId : orderIdList) {
            try {
                this.orderService.cancelOrder(orderId);
                log.info(String.format("orderId:%s 过期,自动关闭", orderId));
            } catch (Exception e) {
                log.info(String.format("orderId:%s 过期,自动关闭失败", orderId), e);
            }
        }
        log.info("======未支付订单过期任务结束");
    }
}
